
var WebNotify = new Object();

WebNotify.appTitle				= "WebNotify";
WebNotify.appID 					= "WebNotify@bablhost.com";
// WebNotify.defaultImage		= "chrome://mozapps/skin/downloads/downloadIcon.png"

/**
* Initialize WebNotify
*/
WebNotify.init = function() {
		/**
		* Init pageLoad handlers [WebNotify.consoleService]
		*/
		var appcontent = document.getElementById("appcontent");   // browser
		if(appcontent)
		  appcontent.addEventListener("DOMContentLoaded", WebNotify.onPageLoad, true);
		var messagepane = document.getElementById("messagepane"); // mail
		if(messagepane)
		  messagepane.addEventListener("load", function () { WebNotify.onPageLoad(); }, true);
	
		/**
		* Init alerts service
		*/
		this.alertsService = Components.classes["@mozilla.org/alerts-service;1"]
			                              .getService(Components.interfaces.nsIAlertsService);
		
		/**
		* Init console service [WebNotify.consoleService]
		*/
		WebNotify.consoleService = Components.classes["@mozilla.org/consoleservice;1"].
		     getService(Components.interfaces.nsIConsoleService);
}


/**
* Test growl function
*/	
WebNotify.notify = function(d, doc) {
	if (d.image == "undefined" || d.image == null)
		d.image = null;
	if (d.name == "undefined" || d.name == null)
		d.name = "Default";
		
	var listener = {
	  observe: function(subject, topic, data) {
			if (topic == "alertclickcallback")
				WebNotify.onNotificationClick(d.name, doc);
	    // WebNotify.log("WN: subject=" + subject + ", topic=" + topic + ", data=" + data + ", name=" + d.name);
	  }
	}
		
	/*
	void showAlertNotification(
	  in AString imageUrl, 
	  in AString title, 
	  in AString text, 
	  [optional] in boolean textClickable,
	  [optional] in AString cookie,
	  [optional] in nsIObserver alertListener,
	  [optional] in AString name  New in Firefox 3                     
	);
	*/

	WebNotify.alertsService.showAlertNotification(d.image, 
		                                    d.title, d.message, 
		                                    true, "cookie", listener);
}


/**
* Handle clicks on notifications
*/
WebNotify.onNotificationClick = function(name, doc) {
	// TODO Impelment WebNotify method
	// WebNotify.notify({title : "Clicked on " + name + "!", message : doc});
	
	// Bring document to front
	if (doc != null)
		doc.defaultView.openDialog("chrome://webnotify/content/dialog.xul", "", "chrome,centerscreen,modal");
	
	// TODO Add click event to document
}


/**
* Make sure strings are formatted correctly to prevent bad commands
*/
WebNotify.safe = function(s) {
	// TODO Impelment WebNotify method
	return s;
}


/**
* Capture notify events and display the growl
*/
WebNotify.notifyEvent = function(evt, doc) {
	WebNotify.notify({
		name	: evt.target.getAttribute("name"),
		title	: evt.target.getAttribute("title"),
		message	: evt.target.getAttribute("message"),
		image		: evt.target.getAttribute("image")
		},
		doc);
}


/**
* Validate URLs on page load, adding WebNotify functionality if it is active
*/
WebNotify.onPageLoad = function(evt) {
	var doc = evt.originalTarget; // doc is document that triggered "onload" event
	
	if (WebNotify.isActiveURL(doc.location.href)) {
		WebNotify.notify({
			title		: WebNotify.appTitle + " Enabled",
			message : doc.location.href
			});
		doc.addEventListener("WNNotifyEvent", function(e) { WebNotify.notifyEvent(e, doc); }, false, true);
	}
}


/**
* Validate URLs
*/
WebNotify.isActiveURL = function(url) {
	// TODO Impelment WebNotify method
	return url == "file:///Users/babl2/Documents/projects/webnotify/webnotify-ff/test/basicAlert.html";
}


/**
*  Log to console
*/
WebNotify.log = function(m) {
		WebNotify.consoleService.logStringMessage(m);
}

window.addEventListener("load", function() { WebNotify.init(); }, false);
